<template>
  <section>
    <el-container class="admin-layout">
      <!--      <page-header :menus="menus" @openMenu="openMenu"></page-header>-->
      <el-container class="page-main">
        <el-main style="padding:0;overflow: hidden">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </section>
</template>
<script>
import PageHeader from "@/components/page-header";

export default {
  name: "Main",
  data() {
    return {
      menus: []
    };
  },
  created() {
    this.getAllMenu();
  },
  methods: {
    openMenu(val) {
      let itemPath = (val + "").replace(/\s*/g, "");
      this.$router.push(itemPath);
    },
    // 获取用户菜单和按钮权限
    async getAllMenu() {
      await this.API.userResource().then(res => {
        let tem = res.data;
        if (tem) {
          this.menus = tem.menuLists;
          //获取上级来文绑定的serverId
          let document = this.menus.find(item => item.key == "gwgl");
          let serverId = null;
          if (document) {
            let superior = document.children.find(
              item1 => item1.key == "sjlw_1"
            );
            if (superior && superior.route) {
              serverId = JSON.parse(superior.route).query
                ? JSON.parse(superior.route).query.serverId
                : null;
            }
          }
          if (serverId) {
            this.API.getOtherSettingByServerId({ serverId: serverId }).then(
              ret => {
                window.sessionStorage.setItem("otherApi", ret.data.apiUrl);
                if (
                  !(
                    window.sessionStorage.getItem("otherApi") == "" ||
                    window.sessionStorage.getItem("otherApi") == undefined ||
                    window.sessionStorage.getItem("otherApi") == null
                  )
                ) {
                  this.otherApi.getOtherAnnouncementCount().then(ret1 => {
                    if (ret1.data) {
                      this.$store.dispatch("updateDocumentCount", {
                        key: "sjlw_1",
                        value: ret1.data
                      });
                    }
                  });
                }
              }
            );
          }
          // else {
          //   this.$store.dispatch("updateDocumentCount", {
          //     key: "sjlw_1",
          //     value: 1
          //   });
          // }
          this.updateUserMenus();
          this.$store
            .dispatch(
              "updateUserRoleKeys",
              tem.buttonLists && tem.buttonLists.length > 0
                ? tem.buttonLists.join(",")
                : ""
            )
            .then(res => {});
          sessionStorage.setItem(
            "roleKeys",
            tem.buttonLists && tem.buttonLists.length > 0
              ? tem.buttonLists.join(",")
              : ""
          );
        }
      });
    },
    updateUserMenus() {
      this.$store.dispatch("updateUserMenus", this.menus);
    }
  },
  components: {
    PageHeader
  }
};
</script>

<style scoped lang="less">
.navbar {
  max-width: 1280px;
  margin: 0 auto;
}

.el-aside {
  color: #333;
}

.el-menu {
  border: none;
}

.admin-layout {
  display: flex;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  overflow: hidden;
  background-color: #f0f7fd;
}

.sidebar {
  background-color: #fff;
  position: relative;
  transition: all 0.3s ease;
  border: solid 1px #e6e6e6;
  box-shadow: 1px 0 15px -5px rgba(0, 0, 0, 0.3);
}

.page-main {
  position: absolute;
  top: 0px;
  left: 0px;
  right: 0;
  bottom: 0;
  background: #f0f7fd;
  transition: all 0.3s ease;
}

.sidebar--show {
  .sidebar {
    transform: translate(0, 0);
  }

  .page-main {
    left: 200px !important;
  }
}

.sidebar--hide {
  .sidebar {
    transform: translate(-200px, 0);
  }

  .page-main {
    left: 0 !important;
  }
}

.collapse-button {
  position: absolute;
  top: calc(50% - 50px);
  left: 0;
  width: 10px;
  height: 100px;
  background: #ccc;
  cursor: pointer;
  border-top-right-radius: 10px;
  border-bottom-right-radius: 10px;
}

.collapse-button:hover {
  background: #dddddd;
}
</style>
